#import "AppDelegate.h"
#import "AppDelegate+GJSetting.h"
#import <React/RCTBridge.h>
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
#import <IQKeyboardManager/IQKeyboardManager.h>

#ifdef FB_SONARKIT_ENABLED
#import <FlipperKit/FlipperClient.h>
#import <FlipperKitLayoutPlugin/FlipperKitLayoutPlugin.h>
#import <FlipperKitUserDefaultsPlugin/FKUserDefaultsPlugin.h>
#import <FlipperKitNetworkPlugin/FlipperKitNetworkPlugin.h>
#import <SKIOSNetworkPlugin/SKIOSNetworkAdapter.h>
#import <FlipperKitReactPlugin/FlipperKitReactPlugin.h>

static void InitializeFlipper(UIApplication *application) {
  FlipperClient *client = [FlipperClient sharedClient];
  SKDescriptorMapper *layoutDescriptorMapper = [[SKDescriptorMapper alloc] initWithDefaults];
  [client addPlugin:[[FlipperKitLayoutPlugin alloc] initWithRootNode:application withDescriptorMapper:layoutDescriptorMapper]];
  [client addPlugin:[[FKUserDefaultsPlugin alloc] initWithSuiteName:nil]];
  [client addPlugin:[FlipperKitReactPlugin new]];
  [client addPlugin:[[FlipperKitNetworkPlugin alloc] initWithNetworkAdapter:[SKIOSNetworkAdapter new]]];
  [client start];
}
#endif

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
#ifdef FB_SONARKIT_ENABLED
  InitializeFlipper(application);
#endif
  // 键盘
  [IQKeyboardManager sharedManager].enable = YES;
  [self initlizations];
  [self amapSetting];
  
  RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
  RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
                                                   moduleName:@"yuanduo_app"
                                            initialProperties:nil];

  rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];

  self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  UIViewController *rootViewController = [UIViewController new];
  rootViewController.view = rootView;
  self.window.rootViewController = rootViewController;
  [self.window makeKeyAndVisible];
  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    [self checkLocalImageToRNUpload];
  });

  return YES;
}

- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
{
#if DEBUG
  return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
#else
  return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
#endif
}

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
  if (self.allowRotation == YES) {
    // 横屏
    return UIInterfaceOrientationMaskLandscape | UIInterfaceOrientationMaskPortrait;
  } else {
    // 竖屏
    return (UIInterfaceOrientationMaskPortrait);
  }
}

- (void)setAllowRotation:(BOOL)allowRotation{
  if (_allowRotation != allowRotation) {
    _allowRotation = allowRotation;
  }
  // 将输入的转屏方向（枚举）转换成Int类型
  int orientation = (int)UIInterfaceOrientationPortrait;
  if (allowRotation) {
    // 将输入的转屏方向（枚举）转换成Int类型
    orientation = (int)UIInterfaceOrientationLandscapeRight;
  }
  NSNumber *orientationUnknown = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
  
  [[UIDevice currentDevice] setValue:orientationUnknown forKey:@"orientation"];
  
  
  
  // 对象包装
  NSNumber *orientationTarget = [NSNumber numberWithInt:orientation];
  
  // 实现横竖屏旋转
  [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
}


- (void)applicationWillEnterForeground:(UIApplication *)application{
  [self checkLocalImageToRNUpload];
}


- (void)checkLocalImageToRNUpload{
  NSString *filePath =  [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex: 0]  stringByAppendingPathComponent:@"waterPhotoDic.plist"];
  NSDictionary *localDic = [NSDictionary dictionaryWithContentsOfFile:filePath];
  if (localDic && localDic.count > 0) {
    [GJUtil sendNotificationForRNEventEmitterWithActionKey:@"APP_NET_CONNECT" parameter:@{}];
  }
  
}
@end
